package com.xhji.spider.examples;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class OschinaProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        System.out.println(page.getHtml());
        page.addTargetRequests(page.getHtml().links().regex("https://gitee\\.com/explore/all\\?lang=Java&page=\\d+").all());
        page.putField("title", page.getHtml().xpath("//div[@class='project-title']//h3//a//text()").toString());
        page.putField("watch", page.getHtml().xpath("//div[@class='explore-project__meta-social pull-right']//a//span//tidyText()"));
        page.putField("desc", page.getHtml().xpath("//div[@class='project-desc']//tidyText()").toString());
        if (page.getResultItems().get("title")==null){
            //skip this page
            page.setSkip(true);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new OschinaProcessor()).addUrl("https://gitee.com/explore/all?lang=Java").run();
    }
}
